package controllers

import (
	"encoding/json"
	"gitee.com/lzqysoft/ims/models"
	"github.com/astaxie/beego"
)

// ImsSubjectController operations for ImsSubject
type ImsSubjectController struct {
	CommonController
}

// URLMapping ...
func (c *ImsSubjectController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Post
// @Description create ImsSubject
// @Param	body		body 	models.ImsSubject	true		"body for ImsSubject content"
// @Success 201 {int} models.ImsSubject
// @Failure 403 body is empty
// @router / [post]
func (c *ImsSubjectController) Post() {
	beego.Debug("")
	var v models.ImsSubject
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if _, err := models.AddImsSubject(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = v
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get ImsSubject by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.ImsSubject
// @Failure 403 :id is empty
// @router /:id [get]
func (c *ImsSubjectController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v, err := models.GetImsSubjectById(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll ...
// @Title Get All
// @Description get ImsSubject
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.ImsSubject
// @Failure 403
// @router /GetAll [post]
func (c *ImsSubjectController) GetAll() {
	tdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrToken)
	}
	var params QueryParams
	if err = json.Unmarshal(tdata, &params); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	l, count, err := models.GetAllImsSubject(params.Query, params.SortBy, params.SortBy, params.Order, (params.Page-1)*params.PageSize, params.PageSize)
	if err != nil {
		c.Data["json"] = err.Error()
		beego.Debug(err)
		c.RetError(ErrInputData)
	}

	c.RetSuccessListAndCount(l, count)
}

// Put ...
// @Title Put
// @Description update the ImsSubject
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.ImsSubject	true		"body for ImsSubject content"
// @Success 200 {object} models.ImsSubject
// @Failure 403 :id is not int
// @router /:id [put]
func (c *ImsSubjectController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v := models.ImsSubject{Id: idStr}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateImsSubjectById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the ImsSubject
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ImsSubjectController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	if err := models.DeleteImsSubject(idStr); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
